package cn.itcast.bos.web.action.base;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.SubArea;
import cn.itcast.bos.service.base.SubAreaService;
import cn.itcast.bos.web.action.common.BaseAction;
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Controller
public class SubAreaAction extends BaseAction<SubArea>{
	
	@Autowired
	private SubAreaService  subAreaService;
	
	@Action(value="subArea_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery() {
		//构造分页查询对象
		Pageable pageable=new PageRequest(page-1, rows);
		//构造条件查询对象
		Specification<SubArea> specification=new Specification<SubArea>() {
			
			@Override
			public Predicate toPredicate(Root<SubArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> subArea=new ArrayList<Predicate>();
				//设置一个区域关联到分区
				//等值查询省
				Join<Object,Object> areaRoot = root.join("area",JoinType.INNER);
				if(model.getArea().getProvince()!=null||StringUtils.isNotBlank(model.getArea().getProvince())) {
					//根据省做等值查询
					Predicate p1=cb.equal(areaRoot.get("provice").as(String.class), 
							model.getArea().getProvince());
					subArea.add(p1);
					
				}
				//模糊查询市
				if(model.getArea().getCity()!=null&&StringUtils.isNotBlank(model.getArea().getCity())) {
					Predicate p2=cb.like(areaRoot.get("city").as(String.class), "%"+model.getArea().getCity()+"%");
					subArea.add(p2);
				}
				//模糊查询区
				if(model.getArea().getDistrict()!=null && StringUtils.isNotBlank(model.getArea().getDistrict())) {
					Predicate p3=cb.like(areaRoot.get("district").as(String.class), "%"+model.getArea().getDistrict()+"%");
					subArea.add(p3);
				}
				
				return cb.and(subArea.toArray(new Predicate[0]));
			}
		};
		//调用业务层
		Page<SubArea> page=subAreaService.findpageData(specification,pageable);
		pushPageDataToValueStack(page);
		return SUCCESS;
		
	}
	//保存分区数据的方法
	@Action(value="subArea_save",results={@Result(name="success",type="redirect",location="./pages/base/sub_area.html")})
	
	public String save() {
		subAreaService.save(model);
		return SUCCESS;
	}
	

}
